﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <ParseChildren(True, ""), ToolboxData("<{0}:ForumRow runat=server></{0}:ForumRow>")> _
    Public Class ForumRow
        Inherits CompositeControl
        Protected hypForumName As New Controls.Link
        Protected plhLastPost As New PlaceHolder
        Protected hypLastPostSubject As New Controls.Link
        Private _forumId As Integer
        Private _forumIcon As String
        Private _rowTemplate As ForumRowControl
        Private _viewRoles As String
        Private _readRoles As String
        Private _userRoles As String
        Private _hidden As Boolean
        Public Overrides ReadOnly Property Controls() As System.Web.UI.ControlCollection
            Get
                EnsureChildControls()
                Return MyBase.Controls
            End Get
        End Property
        <Description("Initial content to render."), DefaultValue(Nothing, ""), Browsable(False), NotifyParentProperty(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty)> _
       Public Property Content() As ForumRowControl
            Get
                EnsureChildControls()
                Return _rowTemplate
            End Get
            Set(ByVal Value As ForumRowControl)
                _rowTemplate = Value
            End Set
        End Property
        Public Property ForumId() As Integer
            Get
                Return _forumId
            End Get
            Set(ByVal value As Integer)
                _forumId = value
            End Set
        End Property
        Public Property ForumIcon() As String
            Get
                Return _forumIcon
            End Get
            Set(ByVal value As String)
                _forumIcon = value
            End Set
        End Property
        Public Property ReadRoles() As String
            Get
                Return _readRoles
            End Get
            Set(ByVal value As String)
                _readRoles = value
            End Set
        End Property
        Public Property ViewRoles() As String
            Get
                Return _viewRoles
            End Get
            Set(ByVal value As String)
                _viewRoles = value
            End Set
        End Property
        Public Property UserRoles() As String
            Get
                Return _userRoles
            End Get
            Set(ByVal value As String)
                _userRoles = value
            End Set
        End Property
        Public Property Hidden() As Boolean
            Get
                Return _hidden
            End Get
            Set(ByVal value As Boolean)
                _hidden = value
            End Set
        End Property
        Protected Overrides Sub CreateChildControls()
            If Content IsNot Nothing Then
                Controls.Clear()
                Me.Controls.Add(Content)
            End If
        End Sub

        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
            Dim canView As Boolean = Permissions.HasPerm(ViewRoles, UserRoles)
            Dim canRead As Boolean = Permissions.HasPerm(ReadRoles, UserRoles)

            If Not Content Is Nothing Then
                hypForumName = CType(Content.FindControl("hypForumName" & ForumId), Link)
                hypForumName.Enabled = canView

                plhLastPost = CType(Content.FindControl("plhLastPost" & ForumId), PlaceHolder)
                If Not plhLastPost Is Nothing Then
                    plhLastPost.Visible = canView
                End If

                hypLastPostSubject = CType(Content.FindControl("hypLastPostSubject" & ForumId), Link)
                If Not hypLastPostSubject Is Nothing Then
                    hypLastPostSubject.Enabled = canView
                End If

            End If
            If canView Then
                Content.RenderControl(writer)
            ElseIf Hidden = False Then
                Content.RenderControl(writer)
            End If

        End Sub

        Private Sub ForumRow_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            If Context Is Nothing OrElse Page Is Nothing Then
                Return
            End If
            If Content IsNot Nothing Then
                Me.Controls.Add(Content)
            End If
        End Sub
    End Class
    <ToolboxItem(False)> _
  Public Class ForumRowControl
        Inherits Control
    End Class
End Namespace
